/*
 *  $Id: CheckDeck.cs 1217 2010-11-27 10:48:54Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2010 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

using Nereal.Serialization;

namespace Ragima.Events {
    [TypeId("checkdeck")]
    public class CheckDeck : DirectConnectEvent {
        [Serialize, XmlName("cards", "card")]
        private readonly List<CardVersionInfo> _cards;

        public CheckDeck() {
            _cards = new List<CardVersionInfo>();
        }
        public CheckDeck(CardDeck deck): this() {
            foreach (var item in deck)
                _cards.Add(new CardVersionInfo(item.Card));
        }

        protected override void ExecuteInner(DirectConnectScene scene) {
            int count = 0;
            foreach (var info in _cards) {
                var card = CardManager.GetCard(info.Id);
                if (card != null && card.Version == info.Version)
                    count++;
            }
            var check = (count == _cards.Count);
            scene.Mode.SendEvent(new CheckDeckAnswer(check));
        }

        public struct CardVersionInfo {
            [XmlName("id")]
            public string Id;
            [XmlName("version")]
            public int Version;

            public CardVersionInfo(Card card) {
                Id = card.Id;
                Version = card.Version;
            }
        }
    }
}
